/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import core.Course;
import core.User;
import db.JDBC.JdbcDaoFactory;
import db.JDBC.core.CourseJdbcDAOCore;
import db.JDBC.core.UserJdbcDAOCore;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


/**
 *
 * @author Anton
 */

public class CourseBean extends BaseBean
{
    private Long id;
    private Course course;

    @PostConstruct
    public void init()
    {
        String getId = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
        if(getId == null)
            return;
        id = new Long(getId);
        if(id != null)
            course = CourseJdbcDAOCore.getInstance().getCourseById(id);
    }

    public Long getId()
    {
        return id;
    }

    public Course getCourse()
    {
        return course;
    }
    public void addCourse(ActionEvent event)
    {        
        if(course != null)
        {
            Course tmpCourse = JdbcDaoFactory.getInstance().getCourseJdbcDao().getCourseById(course.getId());
            if(tmpCourse != null)
            {
                User currentUser = sessionBean.getCurrentUser();
                if(currentUser != null)
                    UserJdbcDAOCore.getInstance().addCourseToUser(currentUser, tmpCourse);
            }
        }
    }
}
